package com.jy.oms.framework.asyn.task.alarm;

import com.jy.oms.framework.asyn.task.core.ExecutorConfig;
import com.jy.oms.framework.asyn.task.core.ExecutorContext;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * builder
 *
 * @author liuquan
 */
public final class AlarmMessage {
    private boolean fillContent;
    private String title;
    private Map<String, Object> messages = new LinkedHashMap<>();

    private AlarmMessage() {
    }

    public static AlarmMessage create() {
        return new AlarmMessage();
    }

    public AlarmMessage title(String title) {
        this.title = title;
        return this;
    }

    public AlarmMessage add(String desc, Object value) {
        if (!fillContent) {
            ExecutorConfig executorConfig = ExecutorContext.getExecutorConfig();
            messages.put("业务组", executorConfig.getBusinessGroup());
            messages.put("环境", executorConfig.getEnv());
            fillContent = true;
        }

        messages.put(desc, value);
        return this;
    }

    public Map<String, Object> getMessages() {
        return messages;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        if (this.title != null) {
            builder.append(this.title).append("\n");
        }

        for (Map.Entry<String, Object> entry : messages.entrySet()) {
            builder.append(">").append(entry.getKey()).append("：").append(entry.getValue()).append("\n");
        }

        return builder.toString();
    }

}
